﻿

CREATE PROCEDURE [dbo].[balcon_StergeUnitate] 
 @CodUnitate smallint AS
BEGIN
DECLARE @NrDoc AS int
SET @NrDoc = 0

BEGIN TRAN
PRINT '[ParamListe]'  
DELETE FROM [ParamListe] WHERE [CodUnitate] = @CodUnitate AND [Host]<>'0'
IF @@ERROR <>0
    GOTO Eroare 

PRINT '[Arhive]'  
DELETE FROM [Arhive] WHERE [CodUnitate] = @CodUnitate
IF @@ERROR <>0
    GOTO Eroare 
    
PRINT '[Rulaje]'  
DELETE FROM [Rulaje] WHERE [CodUnitate] = @CodUnitate
IF @@ERROR <>0
    GOTO Eroare
     
PRINT '[arhRulaje]'  
DELETE FROM [arhRulaje] WHERE [CodUnitate] = @CodUnitate
IF @@ERROR <>0
    GOTO Eroare


PRINT '[RulajeDoc]'  
DELETE FROM [RulajeDoc] WHERE [CodUnitate] = @CodUnitate
IF @@ERROR <>0
    GOTO Eroare 

PRINT '[arhRulajeDoc]'  
DELETE FROM [arhRulajeDoc] WHERE [CodUnitate] = @CodUnitate
IF @@ERROR <>0
    GOTO Eroare 

    
PRINT '[BalconSetup]'  
DELETE FROM [BalconSetup] WHERE [CodUnitate] = @CodUnitate
IF @@ERROR <>0
    GOTO Eroare 
    
PRINT '[SeriiNumerice]'      
DELETE FROM [SeriiNumerice] WHERE [CodUnitate] = @CodUnitate
IF @@ERROR <>0
    GOTO Eroare 

---initializare bilant           
PRINT '[Bilant]'  
DELETE FROM [Bilant] WHERE [CodUnitate]=@CodUnitate
IF @@ERROR <>0
    GOTO Eroare 

PRINT '[arhBilant]'  
DELETE FROM [arhBilant] WHERE [CodUnitate]=@CodUnitate
IF @@ERROR <>0
    GOTO Eroare 

    
PRINT '[ConturiSpeciale]'  
DELETE FROM [ConturiSpeciale] WHERE [CodUnitate]=@CodUnitate
IF @@ERROR <>0
    GOTO Eroare 
    
PRINT '[ArticoleAliniate]'      
DELETE FROM [ArticoleAliniate] WHERE [CodUnitate]=@CodUnitate
IF @@ERROR <>0
    GOTO Eroare 

PRINT '[arhArticoleAliniate]'      
DELETE FROM [ArticoleAliniate] WHERE [CodUnitate]=@CodUnitate
IF @@ERROR <>0
    GOTO Eroare 


---Conturi corespondente
PRINT '[ConturiCoresp]'  
DELETE FROM [ConturiCoresp] WHERE [CodUnitate]=@CodUnitate
IF @@ERROR <>0
    GOTO Eroare 

--conturi
PRINT '[Conturi]'  
DELETE FROM [Conturi] WHERE [CodUnitate]=@CodUnitate
IF @@ERROR <>0
    GOTO Eroare 

PRINT '[arhConturi]'  
DELETE FROM [arhConturi] WHERE [CodUnitate]=@CodUnitate
IF @@ERROR <>0
    GOTO Eroare 


PRINT '[Jurnale]'             
DELETE FROM [Jurnale] WHERE [CodUnitate] = @CodUnitate
IF @@ERROR <>0
    GOTO Eroare 

PRINT '[RezultatPatrimonial]'  
DELETE FROM [RezultatPatrimonial] WHERE [CodUnitate]=@CodUnitate
IF @@ERROR <>0
    GOTO Eroare 

PRINT '[arhRezultatPatrimonial]'  
DELETE FROM [arhRezultatPatrimonial] WHERE [CodUnitate]=@CodUnitate
IF @@ERROR <>0
    GOTO Eroare 


PRINT '[VenituriSiCheltuieli]'            
DELETE FROM [VenituriSiCheltuieli] WHERE [CodUnitate]=@CodUnitate
IF @@ERROR <>0
    GOTO Eroare 

PRINT '[arhVenituriSiCheltuieli]'            
DELETE FROM [arhVenituriSiCheltuieli] WHERE [CodUnitate]=@CodUnitate
IF @@ERROR <>0
    GOTO Eroare 

    
PRINT '[ConturiSpeciale]'  
DELETE FROM [ConturiSpeciale] WHERE [CodUnitate]=@CodUnitate
IF @@ERROR <>0
    GOTO Eroare 

COMMIT TRAN    
GOTO Gata
Eroare:
  ROLLBACK
  RAISERROR ('Eroare la executia procedurii StergeUnitate',-1,0)
Exista_Documente:
  ROLLBACK
  RAISERROR ('Unitatea nu poate fi stearsa deoarece contine documente!',-1,0)
Exista_Rulaje_Documente:
  ROLLBACK
  RAISERROR ('Unitatea nu poate fi stearsa deoarece contine rulaje - documente!',-1,0)

Gata:

END